layui.use(['layer', 'form', 'table', 'util', 'admin', 'element', 'laydate', 'jquery', 'dropdown'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var admin = layui.admin;
    var element = layui.element;
    var laydate = layui.laydate;
    var dropdown = layui.dropdown;

    //渲染日期组件，如果存在的话
    renderLayDateCp($, laydate);


    //渲染表格
    var postInsTb = table.render({
        elem: '#postTable',
        url: prefix + '/list',
        page: true,
        toolbar: ['<p>',
            '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn ' + addFlag + '"><i class="layui-icon layui-icon-addition"></i>添加</button>&nbsp;',
            '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn ' + removeFlag + '"><i class="layui-icon layui-icon-delete"></i>删除</button>&nbsp;',
            '<button lay-event="export" class="layui-btn layui-btn-sm layui-bg-cyan icon-btn ' + exportFlag + '"><i class="layui-icon layui-icon-export"></i>导出</button>',
            '</p>'].join(''),
        initSort: {
            field: 'postSort'
            , type: 'asc'
        },
        where: {
            fieldName: 'postSort'
            , sortType: 'asc'
        },
        cols: [
            [
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'postCode', title: '岗位编码', sort: true},
                {field: 'postSort', title: '岗位排序', sort: true},
                {field: 'postName', title: '岗位名称', sort: true},
                {
                    field: 'status', title: '状态', templet: function (d) {
                        return showDictLabel(dictSysNormalDisable, d.status);
                    }, sort: true
                },
                {title: '操作', toolbar: '#postTbBar', align: 'center', minWidth: 200}
            ]
        ],
        done: function (res, curr, count) {
            //导出的下拉菜单
            dropdown.render({
                elem: 'button[lay-event=export]'
                , trigger: 'hover'
                , data: [{
                    title: '导出全部（无过滤）'
                    , id: "exportAll"
                }, {
                    title: '导出全部（过滤搜索）'
                    , id: "exportSearch"
                }, {
                    title: '导出当前页数据'
                    , id: "exportCurrent"
                }]
                , click: function (obj) {
                    var id = obj.id;
                    if (id === "exportSearch") {
                        var data = $("#postTbSearchForm").serializeArray();
                        exportExcel(data);
                    } else if (id === "exportAll") {
                        exportExcel({});
                    } else {
                        exportExcelCurrent(table, res.data);
                    }
                }
            });
        }
    });


//表格搜索
    form.on('submit(postTbSearch)', function (data) {
        postInsTb.reload({where: data.field, page: {curr: 1}});
        return false;
    });


//表头工具栏点击事件
    table.on('toolbar(postTable)', function (obj) {
        var data = table.checkStatus('postTable').data;
        if (obj.event === "add") {
            showEditModel();
        } else if (obj.event === "del") {
            data = data.map(function (item) {
                return item.postId;
            });
            deleteData(data);
        }
    });


//表格工具条点击事件
    table.on('tool(postTable)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            deleteData([data.postId]);
        } else if (obj.event === 'edit') {
            showEditModel(data);
        }
    });

//触发排序事件
    table.on('sort(postTable)', function (obj) {
        postInsTb.reload({
            initSort: obj
            , where: {
                fieldName: obj.field
                , sortType: obj.type
            }
        });
    });


//编辑或添加
    function showEditModel(mData) {
        admin.open({
            type: 1,
            area: '550px',
            title: (mData ? '添加' : '修改') + '岗位信息表',
            content: $('#postEditDialog').html(),
            success: function (layero, dIndex) {
                form.render();
                if (mData) {
                    // 回显表单数据
                    form.val('postEditForm', mData);
                }
                // 表单提交事件
                form.on('submit(postEditSubmit)', function (data) {
                    var loadIndex = layer.load(2);
                    admin.req(prefix + (mData ? '/edit' : '/add'), data.field, function (resp) {
                        layer.close(loadIndex);
                        if (resp.code === 0) {
                            layer.close(dIndex);
                            himio.notice.success(resp.msg);
                            postInsTb.reload({page: {curr: 1}});
                        } else {
                            himio.notice.error(resp.msg);
                        }
                    }, 'post');
                    return false;
                });

                // 禁止弹窗出现滚动条
                $(layero).children('.layui-layer-content').css('overflow', 'visible');
            }
        });
    }

//删除
    function deleteData(data) {
        if (data.length === 0) {
            himio.notice.warn('请选择要删除的数据');
            return;
        }
        admin.confirm("确认要删除选中的" + data.length + "条数据吗?", function (index) {
            var loadIndex = layer.load(2);
            admin.req(prefix + "/remove", {
                ids: data.join(",")
            }, function (resp) {
                layer.close(index);
                himio.notice.resp(resp, function () {
                    postInsTb.reload({where: data.field, page: {curr: 1}});
                }, true);
                layer.close(loadIndex);
            }, "post");
        });
    }

    /* 导出 excel 数据 */
    function exportExcel(data) {
        layer.confirm("确定导出所有岗位信息表数据吗？", function (index) {
            himio.notice.loading("正在导出数据，请稍后...");
            admin.req(prefix + "/export", data, function (result) {
                himio.notice.resp(result, function () {
                    layer.close(index);
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.data) + "&delete=" + true;
                    notice.destroy();
                });
            }, "post");
        });
    }

    /* 导出当前页 excel 数据 */
    function exportExcelCurrent(table, exportData) {
        var title = [
            '岗位ID',
            '岗位编码',
            '岗位名称',
            '显示顺序',
            '状态',
            '创建时间'
        ];
        var data = [];
        for (var i = 0; i < exportData.length; i++) {
            data.push([
                exportData[i].postId,
                exportData[i].postCode,
                exportData[i].postName,
                exportData[i].postSort,
                exportData[i].status,
                exportData[i].gmtCreate
            ]);
        }
        table.exportFile(title, data, 'xls');
    }


});
